package middle;

import java.util.Arrays;
import java.util.Comparator;

public class Solution_424 {

    public static void main(String[] args) {
        Solution_424 model = new Solution_424();
        System.out.println();
    }

    public int eraseOverlapIntervals(int[][] intervals) {
        if (intervals.length == 0) {
            return 0;
        }

        Arrays.sort(intervals, new Comparator<int[]>() {
            public int compare(int[] interval1, int[] interval2) {
                return interval1[0] - interval2[0];
            }
        });

        int n = intervals.length;
        int[] f = new int[n];
        Arrays.fill(f, 1);
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                if (intervals[j][1] <= intervals[i][0]) {
                    f[i] = Math.max(f[i], f[j] + 1);
                }
            }
        }
        return n - Arrays.stream(f).max().getAsInt();
    }

//    public int eraseOverlapIntervals(int[][] intervals) {
//        Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
//        int end = intervals[0][1], count = 0;
//        for (int i = 1; i < intervals.length; i++) {
//            if (end <= intervals[i][0]) {
//                end = intervals[i][1];
//            } else if (end <= intervals[i][1]) {
//                count++;
//            } else {
//                count++;
//                end = intervals[i][1];
//            }
//        }
//        return count;
//    }
}
